package app.stock.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyVetoException;
import java.util.Arrays;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;

import app.common.ApplicationLocator;
import app.common.DatabaseManager;
import app.common.GlobalConstants;
import app.common.PropertyUtil;
import app.stock.common.Constants;
import app.stock.common.Stock;
import app.stock.common.Transaction;
import app.stock.server.StockManager;

public class StockManagerClient extends JFrame {
	private static StockManagerClient smClient;
	private JDesktopPane jDesktopPane1;
	private JInternalFrame jInternalFrame1;
	private JToolBar jToolBar1;
	private JButton jButton1;
	private JButton jButton2;
	private JMenuBar jMenuBar1;
	private JMenu jMenu1;
	private JMenu jMenu2;

	private JTabbedPane jTabbedPane1;
	private JPanel jPanel1;
	private JPanel jPanel2;
	StockManager sm; // @jve:decl-index=0:

	public StockManagerClient() {
		super();
		initialize();
		smClient = this;
	}

	public static StockManagerClient getStockManagerClient() {
		return smClient;
	}

	private void initialize() {
		this.sm = new StockManager();
		DatabaseManager.init();
		// this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent ew) {
				DatabaseManager.shutdownDB();
				System.exit(0);
			}

		});
		this.jDesktopPane1 = new JDesktopPane();
		this.getContentPane().add(this.jDesktopPane1, BorderLayout.CENTER);
		this.jDesktopPane1.setBackground(new java.awt.Color(128, 128, 255));
		this.setSize(new Dimension(1024, 768));
		this.jInternalFrame1 = new JInternalFrame();
		this.jDesktopPane1.add(this.jInternalFrame1, JLayeredPane.DEFAULT_LAYER);
		this.jInternalFrame1.setBounds(-7, 0, 1016, 734);
		this.jInternalFrame1.setVisible(true);
		try {
			this.jInternalFrame1.setMaximum(true);
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.jMenuBar1 = new JMenuBar();
		// jInternalFrame1.setJMenuBar(jMenuBar1);
		this.jMenu1 = new JMenu(Constants.label_menuFile);
		this.jMenuBar1.add(this.jMenu1);
		this.jMenu2 = new JMenu("jMenu2");
		this.jMenuBar1.add(this.jMenu2);
		this.jToolBar1 = new JToolBar();
		// jInternalFrame1.getContentPane().add(jToolBar1, BorderLayout.NORTH);
		this.jToolBar1.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
		this.jButton1 = new JButton("jButton1");
		this.jToolBar1.add(this.jButton1);
		this.jButton2 = new JButton("jButton2");
		this.jToolBar1.add(this.jButton2);

		this.jTabbedPane1 = new JTabbedPane();
		this.jInternalFrame1.getContentPane().add(this.jTabbedPane1, BorderLayout.CENTER);
		this.jPanel1 = new TransactionListPanel();
		this.jTabbedPane1.addTab(Transaction.label, null, this.jPanel1, null);
		this.jPanel2 = new SummaryPanel();
		this.jTabbedPane1.addTab(Stock.label, null, this.jPanel2, null);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String appDir = ApplicationLocator.getAppDir();
		System.out.println("appDir=" + appDir);
		if (args != null) {
			for (int i = 0; i < args.length; i++) {
				String[] arg = args[i].split("=");
				PropertyUtil.put(arg[0], arg[1]);
			}
		}
		PropertyUtil.put(GlobalConstants.key_appMode, GlobalConstants.value_appMode_desktop);
		GlobalConstants.listCurrent();
		System.out.println(Arrays.asList("InstalledLookAndFeels="
				+ Arrays.asList(UIManager.getInstalledLookAndFeels())));
		System.out.println(Arrays.asList("DefaultLookAndFeel=" + UIManager.getLookAndFeel()));
		try {
			// String lnf = "javax.swing.plaf.metal.MetalLookAndFeel";
			// lnf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
			// lnf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
			// lnf =
			// "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
			// UIManager.setLookAndFeel(lnf);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		StockManagerClient client = new StockManagerClient();
		client.setVisible(true);
	}
}
